package com.atguigu.tingshu.order.api;

import com.atguigu.tingshu.common.login.GuiguLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.order.service.OrderInfoService;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Tag(name = "订单管理")
@RestController
@RequestMapping("api/order")
@SuppressWarnings({"all"})
public class OrderInfoApiController {

    @Autowired
    private OrderInfoService orderInfoService;

    /**
     * 获取我的订单
     * /api/order/orderInfo/findUserPage/1/10
     */
    @GetMapping("/orderInfo/findUserPage/{page}/{limit}")
    @GuiguLogin
    public Result<Page<OrderInfo>> findUserPage(@PathVariable Integer page,
                                                @PathVariable Integer limit) {
        Long userId = AuthContextHolder.getUserId();
        Page<OrderInfo> page1 = new Page(page, limit);
        page1 = orderInfoService.findUserPage(page1, userId);

        return Result.ok(page1);
    }

    /**
     * 根据订单号获取订单相关信息
     * /api/order/orderInfo/getOrderInfo/{orderNo}
     */
    @GetMapping("/orderInfo/getOrderInfo/{orderNo}")
    @GuiguLogin
    public Result<OrderInfo> getOrderInfo(@PathVariable String orderNo) {
        OrderInfo orderInfo = orderInfoService.getOrderInfo(orderNo);
        return Result.ok(orderInfo);
    }


    /**
     * 提交订单
     * /api/order/orderInfo/submitOrder
     *
     * @return
     */
    @PostMapping("/orderInfo/submitOrder")
    @GuiguLogin
    public Result<Map<String, String>> submitOrder(@RequestBody OrderInfoVo orderInfoVo) {
        Long userId = AuthContextHolder.getUserId();
        Map<String, String> map = orderInfoService.submitOrder(orderInfoVo, userId);
        return Result.ok(map);
    }

    /**
     * 订单确认
     * /api/order/orderInfo/trade
     *
     * @return
     */
    @PostMapping("/orderInfo/trade")
    @GuiguLogin
    public Result<OrderInfoVo> trade(@RequestBody TradeVo tradeVo) {
        Long userId = AuthContextHolder.getUserId();
        OrderInfoVo orderInfoVo = orderInfoService.trade(tradeVo, userId);
        return Result.ok(orderInfoVo);
    }

}

